<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\OrderComment;
use App\Models\Order;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class OrderCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new OrderComment(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->scrollbarX();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);


            $grid->column('id')->sortable();
            $grid->column('username', '姓名');
            $grid->column('nickname', '昵称');
            $grid->column('phone', '手机号');
            // $grid->column('organization', '机构');
            $grid->column('journal_name');
            $grid->column('journal_area');
            // $grid->column('title');
            // $grid->column('title_href');
            // $grid->column('cover_page');
            $grid->column('content')->limit(30);
            $grid->column('status')->switch();
            $grid->column('created_at')->sortable();
            // $grid->column('updated_at')->sortable();

            $grid->model()->orderBy('id', 'desc');
            $grid->quickSearch('username', 'nickname', 'phone')->placeholder('输入姓名/手机号/昵称');
            $grid->model()->orderBy('id', 'desc');
            // $grid->filter(function (Grid\Filter $filter) {
            // $filter->equal('id');
            // });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new OrderComment(), function (Show $show) {
            $show->field('id');
            $show->field('username');
            $show->field('nickname');
            $show->field('organization');
            $show->field('phone');
            $show->field('status')->using(OrderComment::status());
            $show->field('journal_name');
            $show->field('journal_area');
            $show->field('title');
            $show->field('title_href');
            $show->field('cover_page');
            $show->field('content');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new OrderComment(), function (Form $form) {
            $form->display('id');
            // $form->select('order_id', '订单')->options(function () {
            //     $options = [];
            //     Order::where('order_status', '=', 2)
            //         ->get()
            //         ->each(function ($row) use (&$options) {
            //             $options[$row->id] = $row->order_name;
            //         });
            //     return $options;
            // });

            $form->text('username', '姓名')->saveAsString();
            $form->text('nickname', '昵称')->saveAsString();
            $form->text('organization', '机构')->saveAsString();
            $form->text('phone', '手机号')->rules('mobile', ['mobile' => '手机号格式有误'])->saveAsString();
            $form->radio('status', '发布状态')->options(OrderComment::status())->default(1);
            $form->text('journal_name', '文章发表期刊名');
            $form->text('journal_area')->saveAsString();
            $form->text('title')->saveAsString();
            $form->text('title_href')->saveAsString();
            $form->image('cover_page')->move('order/orderComment/' . date('Y/m'))->autoUpload()->saveFullUrl();
            // $form->text('content');
            $form->textarea('content')->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
